"use client"; import { useState, useEffect, use } from "react"; import { useRouter } from "next/navigation"; import Button from "@/components/shared/Button"; import Input from "@/components/shared/Input"; export default function EditApplicationPage({ params }) { const resolvedParams = use(params); const router = useRouter(); const [application, setApplication] = useState(null); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [error, setError] = useState(""); const [isLoading, setIsLoading] = useState(true); const [isSubmitting, setIsSubmitting] = useState(false); const [newKey, setNewKey] = useState(null); useEffect(() => { fetchApplication(); }, []); async function fetchApplication() { try { const response = await fetch( "http://localhost:8080/twirp/applications.ApplicationsService/GetApplication", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token: localStorage.getItem("token"), application_id: resolvedParams.id, }), }, ); const data = await response.json(); if (data.code || data.msg) { throw new Error(data.msg || "Failed to fetch application"); } setApplication(data.application); setName(data.application.name); setDescription(data.application.description); } catch (error) { setError(error.message || "Failed to load application"); console.error("Error:", error); } finally { setIsLoading(false); } } async function handleSubmit(e) { e.preventDefault(); setIsSubmitting(true); setError(""); try { const response = await fetch( "http://localhost:8080/twirp/applications.ApplicationsService/UpdateApplication", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token: localStorage.getItem("token"), application_id: resolvedParams.id, name, description, }), }, ); const data = await response.json(); if (data.code || data.msg) { throw new Error(data.msg || "Failed to update application"); } router.push(`/dashboard/applications/${resolvedParams.id}`); } catch (error) { setError(error.message || "Failed to update application"); console.error("Error:", error); } finally { setIsSubmitting(false); } } async function handleRegenerateKey() { if ( !confirm( "Are you sure you want to regenerate the API key? The old key will stop working immediately.", ) ) { return; } try { const response = await fetch( "http://localhost:8080/twirp/applications.ApplicationsService/RegenerateKey", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token: localStorage.getItem("token"), application_id: resolvedParams.id, }), }, ); const data = await response.json(); if (data.code || data.msg) { throw new Error(data.msg || "Failed to regenerate key"); } setNewKey(data.key); } catch (error) { setError(error.message || "Failed to regenerate key"); console.error("Error:", error); } } if (isLoading) return
Loading...
; if (!application) return
Application not found
; return (
{newKey && (

New API Key Generated

Please copy your new API key now. You won't be able to see it again!

{newKey}
)}

Edit Application

{error && (
{error}
)}
setName(e.target.value)} required />